<template>
  <view class="index-all">
      <view class="index-hint2">
          <view class="index-name"><span>{{user ? user.nickName : '--'}}，</span> <text>欢迎登录执法检查系统</text></view>
          <view class="index-unit">{{user && user.org  ? user.org.orgName : '--'}}</view>
      </view>
      <view class="tool-all">
          <view class="index-menu" v-if="applicationList.length > 0">
              <view class="index-menu-list" v-for="(item, index) in applicationList" :key="index"
                    @click="clickTo(item)">
                  <view class="circle" v-if="item.dataCount > 0">
                    <text class="number">{{ item.dataCount > 99 ? '99+' : item.dataCount }}</text>
                  </view>
                  <image class="index-menu-image" :src="imgMinioPath + item.query"></image>
                  <view class="index-menu-text">
                      {{item.menuName ? item.menuName : '--'}}
                  </view>
              </view>
              <view class="index-menu-list" v-if="applicationList.length >=7">
                  <image class="index-menu-image" :src="imgMinioPath + '/law/index-more.png'"
                         @click="clickToMore()"></image>
                  <view class="index-menu-text">
                      更多
                  </view>
              </view>
          </view>
          <view class="index-menu" v-else>
            <view class="index-menu-list" v-for="(item, index) in menuButtons" :key="index"  @click="clickTo(item)" >
              <view class="circle" v-if="item.dataCount > 0">
                <text class="number">{{ item.dataCount > 99 ? '99+' : item.dataCount }}</text>
              </view>
              <image class="index-menu-image" :src="imgMinioPath + item.query"></image>
              <view class="index-menu-text">
                {{item.menuName ? item.menuName : '--'}}
              </view>
            </view>
            <view class="index-menu-list" v-if="menuButtons.length >=7">
              <image class="index-menu-image" :src="imgMinioPath + '/law/index-more.png'" @click="clickToMore()"></image>
              <view class="index-menu-text">
                更多
              </view>
            </view>
          </view>
      </view>
    <Tabbar type="school"></Tabbar>
  </view>
</template>

<script setup>
import {reactive, ref, getCurrentInstance, watchEffect,watch} from 'vue';
import supLawApi from "@/api/law/supLaw/supLaw";
import supTaskApi from "@/api/law/supTask/supTask";
import config from '@/common/config';
import {onLoad, onShow} from '@dcloudio/uni-app'
import api from '@/api/user/user'
const {proxy} = getCurrentInstance();
let user = ref({});
let menus = ref([]);
let menuButtons = ref([]);

let imgMinioPath = ref(config.IMG_MINIO_PATH);
const current = ref(0);

// 获取用户菜单
let initMenu = async (e) => {
  let list = uni.getStorageSync('menus');
  console.log(list)
  menus.value = [];
  menus.value = list.filter(item => {
    if (item.menuSource.indexOf(config.SCHOOL_MENU_SOURCE) !== -1) {
      return item;
    }
  })
  menuButtons.value = [];
  menus.value.forEach((item) => {
    menuButtons.value.push(...item.children)
  });
  menuButtons.value = menuButtons.value.slice(0, 7);
  //查找气泡数量
  let checkMenuIds = config.CHECK_MENU_IDS
  menuButtons.value.forEach(menu => {
    if (checkMenuIds.includes(menu.id)) {
      searchCount(menu);
    }
  });

};


let applicationList = ref([]);
// 获取我的工作台列表
let getApplicationList = async (e) => {
  let queryForm = {};
  applicationList.value = [];
  let res = await proxy.http.asyncPost(api.getApplicationList, queryForm);
  if (res.code === 0) {
    let list = res.data;
    applicationList.value = list.filter(item => {
      if (item.menuSource.indexOf(config.SCHOOL_MENU_SOURCE) !== -1) {
        return item;
      }
    })
    //查找气泡数量
    let checkMenuIds = config.CHECK_MENU_IDS
    applicationList.value.forEach(menu => {
      if (checkMenuIds.includes(menu.id)) {
        searchCount(menu);
      }
    });
    if(!applicationList.value || applicationList.value.length == 0) {
      initMenu();
    }
  }
}

const queryForm = reactive({
  params:{
    lawStatusList:[]
  },
  lawDetail: {
    schoolPropertiesCode: '', // 办学性质
    doSchoolTypeCode: '', // 办学类型
    countyId: '', // 地区
  },
});
const userInfo = ref(uni.getStorageSync('userInfo'));

// 首页气泡数
let searchCount = async (item) => {
  queryForm.pageNum = -1;
  queryForm.pageSize = -1;
  queryForm.createOrgId = userInfo.value.user.orgId;
  console.log("+++++++++++++++")
  console.log(config.CHECK_MENU_IDS)
  console.log(item.id === config.CHECK_MENU_IDS[7])
  console.log(item.id === config.CHECK_MENU_IDS[8])
  console.log(item.id === config.CHECK_MENU_IDS[9])
  if(item.id == config.CHECK_MENU_IDS[0]){//校园专项检查
    item.dataCount = await getSupLawPage('2','2');
  }else if(item.id == config.CHECK_MENU_IDS[1]){//校园日常检查
    item.dataCount = await getSupLawPage('2','1');
  }else if(item.id == config.CHECK_MENU_IDS[2]){//食品安全检查
    item.dataCount = await getSupLawPage('1','1');
  }else if(item.id == config.CHECK_MENU_IDS[3]){//配餐单位检查
    item.dataCount = await getSupLawPage('6','1');
  }else if(item.id == config.CHECK_MENU_IDS[4]){//校园自查
    item.dataCount = await getSupLawPage('4');
  }else if(item.id == config.CHECK_MENU_IDS[5]){//食堂自查
    item.dataCount = await getSupLawPage('3');
  }else if(item.id == config.CHECK_MENU_IDS[6]){//隐患自查
    item.dataCount = await getSupLawPage('5');
  }else if(item.id == config.CHECK_MENU_IDS[7]){//有害生物专项
    item.dataCount = await getSupLawPage('7','2');
  }else if(item.id == config.CHECK_MENU_IDS[8]){//有害生物日常
    item.dataCount = await getSupLawPage('7','1');
  }else if(item.id == config.CHECK_MENU_IDS[9]){//有害生物自查
    item.dataCount = await getSupLawPage('8');
  }
};

async function getSupLawPage(entryType,lawType) {
  queryForm.entryType = entryType;
  if(lawType){
    queryForm.lawStatus = null
    queryForm.params.lawStatusList = ["10","20","30"];
    queryForm.lawType = lawType;
  }else {
    queryForm.lawStatus = 20;
    queryForm.params.lawStatusList = []
    queryForm.lawType = null
  }
  let res = await proxy.http.asyncPost(supLawApi.getsupLawPage, queryForm);
  if (res.code === 0) {
    // if (lawType === '2') {
    //   let query = {
    //     finish_flag: '0'
    //   }
    //   let res2 = await proxy.http.asyncPost(supTaskApi.getSupTaskPage, query);
    //   if (res2.code === 0) {
    //     return res2.data.records.length + res.data.records.length;
    //   } else {
    //     await uni.showToast({title: res.msg, icon: 'none'});
    //     return 0;
    //   }
    // }
    return res.data.records.length || 0;
  } else {
    await uni.showToast({title: res.msg, icon: 'none'});
    return 0;
  }
}

let clickTo = async (item) => {
  if (item.path){
    uni.navigateTo({
      url: item.path
    })
  }else {
    uni.showToast({title: '敬请期待!', icon: 'none'});
  }
};

const {tabbar} = proxy.store();

let clickToMore = async () => {
  tabbar.$patch(state => {
    state.name = 'applicationCenter';
  })
  uni.switchTab({
    url: '/pages/application/applicationCenter'
  });
};

// 获取路由传参
onLoad((option) => {
  user.value = uni.getStorageSync("userInfo").user;
});
let code = ref("10");
onShow(() => {
  tabbar.$patch(state => {
    state.type = 'law';
  })
  user.value = uni.getStorageSync("userInfo").user;
  getApplicationList();
  uni.removeStorageSync('entryType');
})
// #ifdef MP-WEIXIN
uni.hideTabBar();
// #endif
</script>

<style lang="scss" scoped>
.index-all {
  justify-content: center;
  width: 100vw;
  height: 100vh;
  overflow: auto;
  position: relative;
    background:#f9f8fa url("@/static/image/index/login.jpg") no-repeat;
    background-size: 100%;
    .index-hint2{
        margin: 370rpx 30rpx 0 30rpx;
        background: linear-gradient(180deg, #dae9f7 0%, #e6ebf7 100%);
        border-radius: 16rpx 16rpx 0 0;
        padding: 20rpx;
        .index-name{
            span{
              font-size: 31rpx;
              font-weight: bold;
              color: #333333;
            }
            text{
                font-weight: 500;
                font-size: 29rpx;
            }
        }
        .index-unit{
            color: #8B8B8B;
            font-size: 24rpx;
            margin-top: 10rpx;
        }
    }
    .tool-all {
        margin: 0 20rpx;
        background: linear-gradient(180deg, #B8DDFA 0%, #FFFFFF 100%);
        border-radius: 16rpx;
        padding-bottom: 40rpx;
        overflow: auto;
    }
}
  .index-menu {
      margin: 0 auto;
      position: relative;
      overflow: hidden;
      border-radius: 0 0 16rpx 16rpx;
      padding-top: 20rpx;
    .index-menu-list {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      width: 25%;
      height: 180rpx;
      float: left;
      position: relative;
      .index-menu-image {
          width: 110rpx;
          height: 110rpx;
      }
        .index-menu-text {
            font-size: var(--uni-font-size-12);
            color: #333333;
            margin-top: 20rpx;
        }
    }
  }
.circle {
  width: 40rpx;
  height: 40rpx;
  border-radius: 50%;
  background-color: red;
  top: 0;
  right: 10rpx;
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1;
}

.number {
  color: white;
  font-size: 22rpx;
}
</style>
